/**
 * WordGuessGameTwo.java
 * 
 * Player class is used by the WordGameTournament class to play the 
 * second word guessing game.
 * 
 * CSCE 155 Spring 2010
 * Assignment 2
 * @author Josh Branchaud
 * @version 1.0, 2/3/2010
 */

// import statements
import java.io.*;
import java.util.*;

public class WordGuessGameTwo
{

	// private data members -- variables   
	private String secret;
	private String hint;
	private long time;
	
	// another variable to remove later
	private String hello = "helloworld";

	//*** Constructor(s) ***//

    public WordGuessGameTwo()
    {
        this.secret = "";
        this.hint = "";
        this.time = 0;
    }

    //*** end of Constructor(s) ***//


    /**
     * playGameTwo is a method that will be called by WordGuessTournament in
     * order to execute the methods involved in playing Word Jumble.
     * @param secretWord, whoseFirst
     * @return String
     */
    public String playGameTwo(String secretWord, int whoseFirst)
    {
        // Initialize secret to the given secretWord String.
        this.secret = secretWord;

        String outcome = ""; // String containing win/lose depending on outcome
        String guess = ""; // the player's guess at the jumbled word.

        // Generate a hint based on the secret
        this.generateHint();

        // Prompt the user to start playing
        System.out.println("The secret word has been jumbled and you only have one");
        System.out.println("guess to figure out what it is. The timer starts now, so");
        System.out.println("you better guess quickly!");
        System.out.println("Jumbled Word: " + this.hint);

        // Start the timer
        long startTime = System.nanoTime();

        // Allow one input from the player
        guess = this.readString();

        // End the timer
        long endTime = System.nanoTime();

        guess = guess.toUpperCase();

        // Store the time taken in time
        this.time = endTime - startTime;

        if(guess.compareTo(this.secret) == 0)
        {
            System.out.println("Bingo! It is " + this.secret + ". You guessed correctly!");
            outcome = "win";
        }
        else
        {
            System.out.println("Sorry, you didn't guess correctly.");
            outcome = "lose";
        }

        //System.out.println("Time taken: " + this.time);

        return outcome;
    }

    /**
     * generateHint is a method that will generate a hint which is a jumbled
     * version of the secret.
     * @param None
     * @return void
     */
    public void generateHint()
    {
        // Create a string buffer that contains the secret
        StringBuffer tempSecret = new StringBuffer(this.secret);

        // Ensure that hint is an empty string
        this.hint = "";

        System.out.println("Length " + tempSecret.length());

        //
        while(tempSecret.length() > 0)
        {
            // generate a random int based on the buffer's length
            Random ran = new Random();
            int ranInt = ran.nextInt(tempSecret.length());

            // put the random char in hint and remove it from the buffer
            this.hint = this.hint + tempSecret.charAt(ranInt);
            tempSecret.deleteCharAt(ranInt);
        }
    }

    //*** Getter Methods for Private Variables ***//

    /**
     * getTime retrieves the long object represented by time.
     * @param None
     * @return long
     */
    public long getTime()
    {
        return this.time;
    }

    //*** Getter Methods ***//


    //*** Setter Methods for Private Variables ***//

    // As of yet, I don't think any of these will be needed for this project.

    //*** end of Setter Methods ***//


    /**
     * This method reads in a string and returns that string to the caller of
     * this method.
     */
    private String readString()
    {

        String userInput = "";
        Scanner scanner = new Scanner(System.in);

        userInput = scanner.nextLine();

        return userInput;

    }  // end readString

} //end of class WordGuessGameTwo
